package com.itboye.sunsun.person.ui;

import com.itboye.sunsunhome.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.support.BaseDialogFragment;
import com.itboye.sunsun.utils.SPUtils;
import com.tencent.connect.share.QQShare;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.open.utils.Util;
import com.tencent.open.yyb.AppbarAgent;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;
import android.annotation.SuppressLint;
import android.app.DialogFragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

@SuppressLint("NewApi")
public class InviteShareFragment extends BaseDialogFragment implements OnClickListener {

	private static final String APP_ID = "wxa30658c4f4f6bbf4";
	private IWXAPI api;
	private String id;
	private String type;
//	private String AppUrl="http://sunsun.itboye.com/index.php/Webview/Index/invitationAward?";
	private String uid;
	private String password;
	private String url;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// 取消标题栏
		
		// setStyle(DialogFragment.STYLE_NO_FRAME,R.style.shareDialog);

		url=getTag();
		// 设置底部显示
//		uid = "uid" + "=" + (String) SPUtils.get(App.ctx, null, SPContants.USER_ID, "");
//		password = "psw" + "=" + (String) SPUtils.get(App.ctx, null, SPContants.PASSWORD, "");
		//设置监听事件
		View view = inflater.inflate(R.layout.share_fragment, container);
		RelativeLayout sinaRela = (RelativeLayout) view
				.findViewById(R.id.sinarela);
		sinaRela.setOnClickListener(this);
		mTencent = Tencent.createInstance(APPID, getActivity());
		mTencent.startAppbar(getActivity(), AppbarAgent.TO_APPBAR_DETAIL);
		RelativeLayout wxRela = (RelativeLayout) view.findViewById(R.id.werela);
		wxRela.setOnClickListener(this);
		RelativeLayout wecircleRela = (RelativeLayout) view
				.findViewById(R.id.wecirclerela);
		wecircleRela.setOnClickListener(this);
		api = WXAPIFactory.createWXAPI(App.ctx, APP_ID);
		
		
		
		return view;
	}




	@Override
	public void onClick(View v) {
		int id = v.getId();
		UMImage image = new UMImage(App.ctx,
				"http://www.umeng.com/images/pic/social/integrated_3.png");
		switch (id) {
		case R.id.sinarela:
//			 /** shareaction need setplatform , callbacklistener,and content(text,image).then share it **/
//            new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.QQ).setCallback(umShareListener)
//                    .withText("hello umeng video")
//                    .withMedia(image)
//                    .share();
			shareToQQ();
            break;
		case R.id.werela:
			
			WXWebpageObject webpage0=new WXWebpageObject();
			webpage0.webpageUrl=url;
			//标题
			WXMediaMessage msg0=new WXMediaMessage(webpage0);
			msg0.title="森森休闲";
			
			//商品图片
			Bitmap thumb0=BitmapFactory.decodeResource(getResources(),R.drawable.ic_logo);
			msg0.setThumbImage(thumb0);
			
			//构建一个请求
			SendMessageToWX.Req req0=new SendMessageToWX.Req();
			req0.message=msg0;
			req0.scene=SendMessageToWX.Req.WXSceneSession;
			req0.transaction="sessionwebpage";
			api.sendReq(req0);
			
			break;
		case R.id.wecirclerela:
			
			
			//请求网址
			WXWebpageObject webpage=new WXWebpageObject();
			
			//传入的网址
			webpage.webpageUrl=url;
			
			
			//标题
			WXMediaMessage msg=new WXMediaMessage(webpage);
			msg.title="森森休闲";
			
			//商品图片
			Bitmap thumb=BitmapFactory.decodeResource(getResources(),R.drawable.ic_logo);
			msg.setThumbImage(thumb);
			
			//构建一个请求
			SendMessageToWX.Req req=new SendMessageToWX.Req();
			req.message=msg;
			req.scene=SendMessageToWX.Req.WXSceneTimeline;
			req.transaction="webpage";
			api.sendReq(req);
			break;

		default:
			break;
		}
	}
	Tencent mTencent;
	private final String APPID="1105251111";
	private void shareToQQ() {
		// TODO Auto-generated method stub
		 final Bundle params = new Bundle();
		    params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
		    params.putString(QQShare.SHARE_TO_QQ_TITLE, "森森休闲");
		    params.putString(QQShare.SHARE_TO_QQ_SUMMARY,  "邀请有好礼");
		    params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,  url);
		    params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,"http://imgcache.qq.com/qzone/space_item/pre/0/66768.gif");
		    params.putString(QQShare.SHARE_TO_QQ_APP_NAME,  "森森休闲");
//		    params.putInt(QQShare.SHARE_TO_QQ_EXT_INT,  "其他附加功能");		
		    mTencent.shareToQQ(getActivity(), params,new IUiListener() {
				
				@Override
				public void onError(UiError arg0) {
					// TODO Auto-generated method stub
					Toast.makeText(getActivity(), "分享失败，请重试", 0).show();
				}
				
				@Override
				public void onComplete(Object arg0) {
					// TODO Auto-generated method stub
					Toast.makeText(getActivity(), "分享完成", 0).show();
				}
				
				@Override
				public void onCancel() {
					// TODO Auto-generated method stub
					Toast.makeText(getActivity(), "已取消", 0).show();
				}
			});
	}

	private UMShareListener umShareListener = new UMShareListener() {
		@Override
		public void onResult(SHARE_MEDIA platform) {
			// Toast.makeText(ShareActivity.this, platform + " 分享成功啦",
			// Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onError(SHARE_MEDIA platform, Throwable t) {
			// Toast.makeText(ShareActivity.this,platform + " 分享失败啦",
			// Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onCancel(SHARE_MEDIA platform) {
			// Toast.makeText(ShareActivity.this,platform + " 分享取消了",
			// Toast.LENGTH_SHORT).show();
		}
	};

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		UMShareAPI.get(App.ctx).onActivityResult(requestCode, resultCode, data);
	}

}
